import { ResponseData, SUCCESS_CODE } from '@/utils/request';
import type { RequestData } from '@ant-design/pro-table';
import { request } from 'umi';
import { SystemConfigItem } from './types';
import { message } from 'antd';

/**
 * 分页查询客户系统配置
 * @param params 
 * @returns 
 */
export async function pageQuerySystemConfig(params: {
  tenant_id?: string,
  page?: number,
  pageSize?: number
}): Promise<RequestData<SystemConfigItem>> {
  const res = await request('/platform_config/view_config', {
    params: {
      page: 1,
      pageSize: 10,
      ...params,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }
  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}

/**
 * 查看系统配置
 * @param tenant_id 
 * @returns 
 */
export async function getSystemConfig(
  tenant_id: string
): Promise<RequestData<SystemConfigItem>> {
  const res = await request('/platform_config/get_config', {
    params: {
      tenant_id: tenant_id
    },
  })
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, data: undefined };
  }
  return {
    success: true,
    data: res.data,
  };

}

/**
 * 更新客户系统配置
 * @param data 
 * @returns 
 */
export async function editSystemConfig(data: SystemConfigItem): Promise<ResponseData<any>> {
  const res = await request('/platform_config/modify', {
    method: 'POST',
    data: {
      id: data.id,
      believe_degree: data.believe_degree,
      fail_repush: data.fail_repush,
      tenant_id: data.tenant_id
    },
  });
  if (res) return { code: res.code, data: res.data, success: true, message: res.message };
  return res;
}
